package com.zatom.newscore.config;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2Configuration {
  @Value("${doc.title:业务接口文档}")
  private String title;

  @Bean
  public Docket createRestApi() {
    ApiInfo apiInfo = (new ApiInfoBuilder()).title(this.title).description(this.title + "，符合RESTful API。").version("1.0.0").build();
    Predicate<RequestHandler> basePackage = RequestHandlerSelectors.basePackage("com.zatom.platform");
    List<ResponseMessage> globalResponses = Arrays.asList(new ResponseMessage[] { (new ResponseMessageBuilder())
          .code(500).message("error").build(), (new ResponseMessageBuilder())
          .code(200).message("success").build() });
    return (new Docket(DocumentationType.SWAGGER_2))
      .globalResponseMessage(RequestMethod.GET, globalResponses)
      .globalResponseMessage(RequestMethod.POST, globalResponses)
      .globalResponseMessage(RequestMethod.DELETE, globalResponses)
      .globalResponseMessage(RequestMethod.PUT, globalResponses)
      .apiInfo(apiInfo)
      
      .useDefaultResponseMessages(false)
      .select()
      .apis(basePackage)
      .paths(PathSelectors.any())
      .build();
  }
}
